# name => 半自动抢课助手
# author => XiaoRan
# status => 功能优化升级中
# auto => False/half
# blog => https://www.lzxr.vip
# QQ => 2897406734

import requests
import time
from urllib import parse
SUBMIT_URL = "http://jwgl.cuit.edu.cn/eams/stdElectCourse!batchOperator.action?profileId=4130"
headers={
    "Referer": "http://jwgl.cuit.edu.cn/eams/stdElectCourse!defaultPage.action?electionProfile.id=3717",
    "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
    "Cookie": "semester.id=403; JSESSIONID=F5453A925952E83B228741B82E25CC85; GSESSIONID=F5453A925952E83B228741B82E25CC85",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.54",
}
ID_LIST = ["89366","89380", "89381"]
apoch = 1
print("开始")
while True:
    if apoch > 9999999:
        apoch = 1
    apoch = apoch + 1
    time.sleep(0.5)
    for id in ID_LIST:
        request_body = "optype=true&operator0="+id+":true:0&lesson0="+id+"&schLessonGroup_"+id+"=undefined"
        data = dict(parse.parse_qsl(request_body))
        rst = requests.request(url=SUBMIT_URL, method="POST", headers=headers, data=data, verify=False)
        content = rst.text
        if apoch % 2 == 0:
            timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
            if "已满" in content:
                print(id + " 人数已满 " + timestamp)
            elif "冲突" in content:
                print(id + " 课程冲突 " + timestamp)
            elif "成功" in content:
                print(id + " 抢课成功 " + timestamp)
                exit(1)
            elif "身份认证" in content:
                print("session过期，请重新获取 " + timestamp)
            elif "已经" in content:
                print("抢课成功")
                exit(1)
            else:
                print(id + " 抢课失败 " + timestamp)
# Debug & Other-info
# optype=true&operator0=73833%3Atrue%3A0&lesson0=73833&schLessonGroup_73833=undefined
